<?php

defined('SYSPATH') or die('Access error!');

class Meta
{

    private $title = "";
    private $keywords = "";
    private $description = "";
    private $web_name = '氿上网';
    private $title_separtor = ' - ';
    private $keyword_separtor = ',';
    private $limit_lenght = 30;

    public static function factory($title = '', $keywords = '', $description = '')
    {
        $meta = new Meta;
        if (Arr::validate_arr($title))
        {
            $meta->title = implode($meta->title_separtor, $title);
        }
        else
        {
            $meta->title = $title;
        }

        if (Arr::validate_arr($keywords))
        {
            $meta->keywords = implode($meta->keyword_separtor, $keywords);
        }
        else
        {
            $meta->keywords = $keywords;
        }
        $meta->description = Text::limit_chars($description, $meta->limit_lenght);
        return $meta;
    }

    public function set_title($title = "")
    {
        $this->title = (string) $title;
    }

    public function set_keyword($keywords = "")
    {
        $this->keywords = (string) $keywords;

        return $this;
    }

    public function set_description($description = "")
    {
        $this->description = (string) $description;

        return $this;
    }

    public function render($flag = TRUE)
    {
        
        $extend_title = '';
        if ($flag)
        {
            $extend_title = ' - ' . $this->web_name;
        }
        $str = "<title>" . $this->title . $extend_title . "</title>";
        $str .="<meta name=\"keywords\" content=\"" . $this->keywords . "\" />";
        $str .="<meta name=\"description\" content=\"" . $this->description . "\" />";
        return $str;
    }

}